﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JzSport.Model
{
    public class UserRelationRepository
    {
        private JzSportDbContext _dbContext;

        public UserRelationRepository(JzSportDbContext dbContext)
        {
            _dbContext = dbContext;
        }

        public void Add(UserRelation userRelation)
        {
            _dbContext.Set<UserRelation>().Add(userRelation);
        }

        public void Update(Guid id, UserRelation userRelation)
        {
            _dbContext.Entry<UserRelation>(userRelation).State = System.Data.EntityState.Modified;
        }

        public void Delete(UserRelation userRelation)
        {
            _dbContext.Entry<UserRelation>(userRelation).State = System.Data.EntityState.Deleted;
        }

        public UserRelation Get(int userid, int targetUserId, int relationType)
        {
            return _dbContext.Set<UserRelation>().Where(p => p.UserId == userid
                && p.TargetUserId == targetUserId
                && p.Type == relationType).SingleOrDefault();
        }

        public UserRelation Get(int userid, int targetUserId, int relationType, int relationStatus)
        {
            return _dbContext.Set<UserRelation>().Where(p => p.UserId == userid 
                && p.TargetUserId == targetUserId
                && p.Type == relationType
                && p.Status == relationStatus).SingleOrDefault();
        }

        public List<UserRelation> GetList(int userid, int relationType, int relationStatus, int start, int limit)
        {
            return _dbContext.Set<UserRelation>().Where(p => p.UserId == userid
                && p.Type == relationType
                && p.Status == relationStatus)
                .OrderBy(p=>p.CreatedTime)
                .Take(limit)
                .Skip(start)
                .ToList();
        }

        /// <summary>
        /// 获取用户的好友列表
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public List<Data.Friend> GetFriendList(int userId, int start, int limit)
        {
            return _dbContext.Set<UserRelation>().Where(p => p.UserId == userId
                && p.Status == BasicData.UserRelationStatus.Established
                && p.Type == BasicData.UserRelationType.Friends)
                .Join(_dbContext.Set<Account>(), l => l.TargetUserId, r => r.Id, (l, r) => new Data.Friend()
                {
                    Id = r.Id,
                    Name = r.Name,
                    NickName = r.NickName,
                    AvatarUrl = r.AvatarUrl,
                    Phone = r.Phone,
                    City = r.City,
                    Sex = r.Sex
                }).ToList();
        }

        public int GetFriendCount(int userId)
        {
            return _dbContext.Set<UserRelation>().Where(p => p.UserId == userId
                && p.Status == BasicData.UserRelationStatus.Established
                && p.Type == BasicData.UserRelationType.Friends).Count();
        }
    }
}
